// index.ts
// 获取应用实例
const app = getApp<IAppOption>()
const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'

Component({
  data: {
    motto: 'Hello World',
    
    userInfo: {
      avatarUrl: defaultAvatarUrl,
      nickName: ''
    },
    hasUserInfo: false,
    canIUseGetUserProfile: wx.canIUse('getUserProfile'),
    canIUseNicknameComp: wx.canIUse('input.type.nickname'),
  },
  
  methods: {
    // 事件处理函数
    bindViewTap() {
      wx.navigateTo({
        url: '../logs/logs',
      })
    },
    onChooseAvatar(e: any) {
      const { avatarUrl } = e.detail
      console.log(e.detail)
      const { nickName } = this.data.userInfo
      this.data.userInfo.avatarUrl=e.detail
      this.setData({
        "userInfo.avatarUrl": avatarUrl,
        hasUserInfo: nickName && avatarUrl && avatarUrl !== defaultAvatarUrl,
      })
    },
    onInputChange(e: any) {
      const nickName = e.detail.value
      console.log(e.detail)
      const { avatarUrl } = this.data.userInfo
      this.data.userInfo.nickName=e.detail
      this.setData({
        "userInfo.nickName": nickName,
        hasUserInfo: nickName && avatarUrl && avatarUrl !== defaultAvatarUrl,
      })
    },
// 假设这是在某个页面的方法
getUserProfile() {
  var that = this;
  var code
  // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认，开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
  wx.getUserInfo({
    
    desc: '展示用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
    success: (res) => {
      // 获取用户信息成功
      wx.setStorageSync('userdetail', res);
      var userInfo = res.userInfo;
      var  cloudID=res.cloudID
      var encryptedData=res.encryptedData
      var iv=res.iv
      wx.setStorageSync('userInfo', userInfo);
      if (userInfo.nickName.length > 0) {
        wx.login({
          
          success: function (res) {
            
            //换取openid & session_key
            
            code=res.code
            wx.setStorageSync('code', code);
            wx.request({
              url: "https://9p47q51245.goho.co/text",
              data: {
                code: res.code,
                encryptedData:encryptedData,
                cloudID:cloudID,
                iv:iv
              },
              header: {
                'Content-Type': 'application/json'
              },
              method: "POST",
              success: function (res) {
                console.log('----------------------------------')
                var detail=res.data.detail.openId;
                wx.setStorageSync('openId', detail);
                console.log(detail);
                wx.reLaunch({
                  url: '/pages/me/me'
                });
              },
              fail: function (res) {
                console.log(res);
              }
            });
          }
        });
      }
      console.log(res);

      // 设置页面数据
      this.setData({
        userInfo: userInfo,
        hasUserInfo: true
      });
    }
  }); // 确保这里的括号是闭合的
}

  },

})
